<?php

namespace backend\models\system;

use Yii;
use backend\models\system\SysConfig;

/**
 * ContactForm is the model behind the contact form.
 */
class IndexForm extends SysConfig
{
    public $sitename;   //站点名称
    public $title;
    public $url;
    public $logo;
    public $icon;
    public $keywords;
    public $description;
    public $email;
    public $copyright;
    public $icp;
    public $count;

    /**
     *  验证规则
     */
    public function rules()
    {
        return [
            [['sitename','title'], 'required'],
            [['keywords','description','copyright','icp','count'], 'string'],
            [['logo'], 'image', 'extensions' => 'png, jpg, gif, jpeg', 'minWidth' => 20, 'maxWidth' => 300, 'maxSize' => 1024*50, 'tooBig' => 'LOGO不得超过50k', 'overWidth' => 'LOGO图片宽度不得超出300像素', 'underWidth' => 'LOGO图片宽度不得低于20像素'],
            [['icon'], 'image', 'extensions' => 'png, jpg, gif, ico, jpeg', 'minWidth' => 10, 'maxWidth' => 60, 'maxSize' => 1024*60, 'tooBig' => 'ico图标不得超过10k', 'overWidth' => 'ico图标宽度不得超出60像素', 'underWidth' => 'ico图标宽度不得低于10像素'],
            ['url', 'url', 'defaultScheme' => 'http, https'],
            ['email', 'email'],
        ];
    }

    /**
     * 表单属性
     */
    public function attributeLabels()
    {
        return [
            'sitename' => '站点名称',
            'title' => '网站标题',
            'url' => '网站域名',
            'logo' => '站点LOGO',
            'icon' => '浏览器图标',
            'keywords' => 'SEO关键词',
            'description' => 'SEO描述信息',
            'email' => '管理员邮箱地址',
            'copyright' => '内容版权说明',
            'icp' => 'ICP备案号',
            'count' => '统计分析代码',
        ];
    }
    
    /**
     * 上传logo和icon图片
     */
    public function upload()
    {
        if ($this->validate()) {
            $result = [];
            //上传logo
            if ($this->logo) {
                $logo_file = 'uploads/images/' . uniqid() . '.' . $this->logo->extension;
                $this->logo->saveAs($logo_file);
                $result['logo'] = $logo_file;
            }
            //上传icon
            if($this->icon) {
                $icon_file = 'uploads/images/' . uniqid() . '.' . $this->icon->extension;
                $this->icon->saveAs($icon_file);
                $result['icon'] = $icon_file;
            }

            return $result;
        } else {
            return false;
        }
    }



}